﻿#include "appstore.h"
#include "ui_appstore.h"

appStore::appStore(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::appStore)
{
    ui->setupUi(this);
    setWindowFlag(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);
    QMovie *back_gif=new QMovie(":/store_back.gif");
    ui->label->setMovie(back_gif);
    ui->label->setStyleSheet("background:transparent");
    ui->label->setAttribute(Qt::WA_TransparentForMouseEvents);
    back_gif->start();
    ui->lineEdit->setPlaceholderText("请输入你要下载的软件名字");

    QIcon fanghui(":/appStore.png");
    ui->pushButton_2->setIcon(fanghui);
    ui->pushButton_2->setIconSize(ui->pushButton_2->size());

    QIcon search(":/search.png");
    ui->pushButton->setIcon(search);
    ui->pushButton->setIconSize(ui->pushButton->size());
    ui->pushButton->setStyleSheet("border:none;background:none;");
    ui->pushButton->setShortcut(QKeySequence("Ctrl+l"));
    model=new QStandardItemModel(ui->tableView);
    ui->tableView->setModel(model);
    model->setColumnCount(6); // 确保有足够的列
    ui->label->setStyleSheet("background:transparent");
    ui->label->setAttribute(Qt::WA_TransparentForMouseEvents);
    QIcon con= QIcon(":/music.png");
    QString name="网易云";
    model->appendRow(QList<QStandardItem*>()<<new QStandardItem(con,name)\
                     <<new QStandardItem("201MB")\
                     <<new QStandardItem("4.0分")\
                     <<new QStandardItem(QIcon(":/nodown.png"),"安装")\
                     <<new QStandardItem()\
                     <<new QStandardItem(":/music.png"));
    con=QIcon(":/tiktok.png");
    model->appendRow(QList<QStandardItem*>()<<new QStandardItem(con,"抖音")\
                     <<new QStandardItem("236MB")\
                     <<new QStandardItem("3.4分")\
                     <<new QStandardItem(QIcon(":/nodown.png"),"安装")\
                     <<new QStandardItem()\
                     <<new QStandardItem(":/tiktok.png"));

    con=QIcon(":/telegram.png");
    model->appendRow(QList<QStandardItem*>()<<new QStandardItem(con,"腾讯视频")\
                     <<new QStandardItem("142MB")\
                     <<new QStandardItem("3.2分")\
                     <<new QStandardItem(QIcon(":/nodown.png"),"安装")\
                     <<new QStandardItem()\
                     <<new QStandardItem(":/telegram.png"));

    con=QIcon(":/huiyi.png");
    model->appendRow(QList<QStandardItem*>()<<new QStandardItem(con,"腾讯会议")\
                     <<new QStandardItem("133MB")\
                     <<new QStandardItem("1.4分")\
                     <<new QStandardItem(QIcon(":/nodown.png"),"安装")\
                     <<new QStandardItem()\
                     <<new QStandardItem(":/huiyi.png"));

    con=QIcon(":/weather.png");
    model->appendRow(QList<QStandardItem*>()<<new QStandardItem(con,"懒人天气")\
                     <<new QStandardItem("93.0MB")\
                     <<new QStandardItem("3.9分")\
                     <<new QStandardItem(QIcon(":/nodown.png"),"安装")\
                     <<new QStandardItem()\
                     <<new QStandardItem(":/weather.png"));

    con=QIcon(":/browser.png");
    model->appendRow(QList<QStandardItem*>()<<new QStandardItem(con,"夸克")\
                     <<new QStandardItem("104MB")\
                     <<new QStandardItem("4.3分")\
                     <<new QStandardItem(QIcon(":/nodown.png"),"安装")\
                     <<new QStandardItem()\
                     <<new QStandardItem(":/browser.png"));

    con=QIcon(":/hpjy.png");
    model->appendRow(QList<QStandardItem*>()<<new QStandardItem(con,"和平精英")\
                     <<new QStandardItem("1.93GB")\
                     <<new QStandardItem("3.0分")\
                     <<new QStandardItem(QIcon(":/nodown.png"),"安装")\
                    <<new QStandardItem()\
                     <<new QStandardItem(":/hpjy.png"));

    con=QIcon(":/jcc.png");
    model->appendRow(QList<QStandardItem*>()<<new QStandardItem(con,"金铲铲")\
                     <<new QStandardItem("1.96GB")\
                     <<new QStandardItem("2.1分")\
                     <<new QStandardItem(QIcon(":/nodown.png"),"安装")\
                    <<new QStandardItem()\
                     <<new QStandardItem(":/jcc.png"));

    con=QIcon(":/qt.png");
    model->appendRow(QList<QStandardItem*>()<<new QStandardItem(con,"Qt5.0")\
                     <<new QStandardItem("5.17GB")\
                     <<new QStandardItem("9.99分")\
                     <<new QStandardItem(QIcon(":/nodown.png"),"安装")\
                      <<new QStandardItem()\
                     <<new QStandardItem(":/qt.png"));

    con=QIcon(":/waimai.png");
    model->appendRow(QList<QStandardItem*>()<<new QStandardItem(con,"美团外卖")\
                     <<new QStandardItem("59.5MB")\
                     <<new QStandardItem("3.3分")\
                     <<new QStandardItem(QIcon(":/nodown.png"),"安装")\
                     <<new QStandardItem()\
                     <<new QStandardItem(":/waimai.png"));

    ui->tableView->setColumnHidden(5,true);

}


void appStore::backWidget(QWidget *parent)
{
    back=parent;
}

appStore::~appStore()
{
    delete ui;
}

void appStore::on_tableView_doubleClicked(const QModelIndex &index)
{
    qDebug()<<index.row()<<" "<<index.column();
    if(index.column()==3)//安装
    {
        QStandardItem *item=model->item(index.row(),3);
        QString str=item->text();
        if(str=="安装")
        {
            apps++;//目前只能装20个软件
            QString buttonName=QString("pushButton_%1").arg(apps);
            item=model->item(index.row(),5);
            str=item->text();

            QString appName=model->item(index.row(),0)->text();
            map[appName]=buttonName;

            qDebug()<<appName<<" "<<buttonName;

            model->setItem(index.row(),index.column()+1,new QStandardItem(QIcon(":/delete.png"),"卸载"));
            model->setItem(index.row(),index.column(),new QStandardItem(QIcon(":/download.png"),"已安装"));
            QPushButton* button=back->findChild<QPushButton*>(buttonName);
            QIcon con=QIcon(str);
            button->setIcon(con);
            button->setIconSize(button->size());
            ui->tableView->reset();
        }
        else
        {
            qDebug()<<"已经安装过了";
        }
    }
    else if(index.column()==4)//卸载
    {
        QString str=model->item(index.row(),index.column())->text();
        if(!str.isEmpty())
        {
             apps--;
             QString appName=model->item(index.row(),0)->text();
             QString buttonName=map[appName];
             QPushButton* button=back->findChild<QPushButton*>(buttonName);
             button->setIcon(QIcon());//在桌面上删除它
             map.remove(appName);
             model->setItem(index.row(),3,new QStandardItem(QIcon(":/nodown.png"),"安装"));
             model->setItem(index.row(),4,new QStandardItem());

        }
        else
        {
            qDebug()<<"还没安装呢，大哥";
        }

    }
}

void appStore::on_pushButton_2_clicked()
{
    back->show();
    this->hide();
}

void appStore::on_pushButton_clicked()
{
    model->clear();
    QString appName=ui->lineEdit->text();
    if(sql==nullptr)
    {
        sql=new mySql;
    }
    appInfo info;
    if(sql->getAppInfo(appName,info))
    {
        QStandardItem *item_1=new QStandardItem(QIcon(":/nodown.png"),"安装");
        QStandardItem *item_2=new QStandardItem;
        if(map.contains(appName))//如果安装了
        {
            item_1=new QStandardItem(QIcon(":/download.png"),"已安装");
            item_2=new QStandardItem(QIcon(":/delete.png"),"卸载");
        }
        model->appendRow(QList<QStandardItem*>()<<new QStandardItem(QIcon(info.path),info.name)\
                         <<new QStandardItem(info.memory)\
                         <<new QStandardItem(info.comment)\
                         <<item_1\
                         <<item_2\
                         <<new QStandardItem(info.path));
        ui->tableView->setColumnHidden(5,true);
    }
    else
    {
        qDebug()<<"你需要的软件暂时未加入本小店";
    }

}
